package com.example.demo.ctl;

import com.example.demo.config.domain.UserInfo;
import com.example.demo.dao.UserInfoDAO;
import com.example.demo.service.SendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangting on 31/03/2021.
 */
@RestController
@RequestMapping("/a")
@Slf4j
public class SendCtl {


    @Autowired
    private  UserInfoDAO userInfoDAO;


    @Autowired
    private SendService sendService;
    @GetMapping("/b")
    public String send(@RequestParam("num") String num,@RequestParam("key") String key){
        Integer i = Integer.valueOf(num);
        long s  =System.currentTimeMillis();
        for (int j = 0; j <i; j++) {
            sendService.send(j+key,key);
            log.info("*****send producer msg :{}",i+key);
        }
        log.info("P: {},{}*****send producer msg :{}",Thread.currentThread().getName(), (System.currentTimeMillis()-s)/1000 );

        return "0k";
    }
    @GetMapping("/receive")
    public String receive(@RequestParam("num") String num,@RequestParam("key") String key){
        Integer i = Integer.valueOf(num);
        for (int j = 0; j <i; j++) {
            long s  =System.currentTimeMillis();
            sendService.sendAndReceive(j+key,key);
            log.info("P: {},{}*****send producer msg :{}",Thread.currentThread().getName(), (System.currentTimeMillis()-s),j+key);
        }
        return "0k";
    }


    @GetMapping("/abc")
    public String abc(){
        userInfoDAO.findByInSchool(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,2);

       List list= userInfoDAO.findByInSchoolBetween(new Date(), calendar.getTime());
       if(!CollectionUtils.isEmpty(list)) {
           System.out.println("=======size: " + list.size());
       }else{
           System.out.println("======list=is null: "  );

       }
        UserInfo u = new UserInfo();
        u.setAge(20);
        u.setBornDate(new Date());
        u.setInSchool(calendar.getTime());
        u.setResume("666666666");
        u.setUserGender("1");
        u.setId(1000);
        userInfoDAO.save(u);



        System.out.println("");
        return "0k";
    }


}
